package com.editso.haier.service;

import com.editso.haier.bean.TopicBean;
import com.editso.haier.dao.TopicDao;
import com.editso.haier.entity.Topic;
import com.editso.haier.entity.User;
import com.editso.haier.result.Result;
import com.editso.haier.result.ResultCode;
import com.editso.haier.result.topic.SimpleTopic;
import com.editso.haier.util.Pair;
import com.editso.haier.util.Promise;
import com.editso.haier.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.Set;

/**
 * 主题的相关操作
 */
@Service
public class TopicService {
    /**
     * 主题持久化
     */
    private TopicDao topicDao;

    @Autowired
    public void setTopicDao(TopicDao topicDao) {
        this.topicDao = topicDao;
    }

    public Result detailsTopic(Integer id, boolean lazy, boolean secondary) {
        return Result.promise(callback->{
            Topic topic = topicDao.findById(id).orElse(null);
            if (topic == null) {
                callback.reject(null);
                callback.error(err->Result.failure("该主题不存在!"));
            }else{
                callback.resolve(topic);
                callback.accept(err->Result.success(SimpleTopic.from(topic, lazy, secondary)));
            }
        }, promise->{

        });
    }

    public Result queryAllTopic(boolean lazy, boolean cascade, Integer page, Integer size, boolean secondary) {
        return page == null || size == null ? queryAll(lazy, cascade, secondary) : queryAllByPage(page, size, secondary);
    }

    private Result queryAll(boolean lazy, boolean cascade, boolean secondary) {
        Iterable<Topic> topics = cascade ? topicDao.findAllByParentNull() : topicDao.findAll();
        Set<SimpleTopic> topicCards = SimpleTopic.from(topics, lazy, secondary);
        return Result.success(topicCards);
    }

    private Result queryAllByPage(Integer page, Integer size, boolean secondary) {
        return null;
    }

    /**
     * 新增一个主题
     *
     * @param topicBean 对其传入的参数
     *                pid: 父级
     *                name: 主题名
     *                desc: 描述信息
     */
    public Result insertTopic(TopicBean topicBean, String userId) {
        if (topicDao.existsTopicByName(topicBean.getName()))
            return Result.result("该主题已存在!", ResultCode.EXISTS);
        Topic topic = new Topic();
        topic.setUser(new User(userId));
        topic.setName(topicBean.getName());
        topic.setDescription(topicBean.getDesc());
        topic.setParent(topicBean.getPid() == null ? null : topicDao.findById(topic.getId()).orElse(null));
        return topicDao.save(topic) != null ? Result.success(topicBean) : Result.failure();
    }

    public Result cascadeInsertTopic(TopicBean topicBean, String userId) {
        System.out.println(topicBean);
        return Result.promise(callback->{
            Topic topic = topicDao.findByName(topicBean.getName()).orElse(new Topic(topicBean.getName()));
            topic.setUser(new User(userId));
            cascadeSaveTopic(topic, topicBean.getChildren());
            topicDao.save(topic);
        }, promise -> {});
    }

    public void cascadeSaveTopic(Topic topic, Set<TopicBean> topics){
        if (topics == null) return;
        Topic cTopic = null;
        Set<Topic> children = topic.getChildren();
        for (TopicBean bean : topics) {
            cTopic = topicDao.findByName(bean.getName()).orElse(new Topic(bean.getName()));
            cTopic.setUser(topic.getUser());
            children.add(cTopic);
            cascadeSaveTopic(cTopic, bean.getChildren());
        }
    }
}
